package com.yinhe.music.yhmusic.singer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yinhe.music.yhmusic.R;

/**
 * Created by Johnson on 2018/3/11.
 */

public class SlideBar extends View {
    public interface OnTouchAssortListener{
        public void onTouchAssortListener(String s);
    }

    // 分类
    private static final String[] ASSORT_TEXT_1 = {"A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z" ,"#"};
    private static final String[] ASSORT_TEXT_2 = {"$", "热", "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z" ,"#"};
    private static String[] ASSORT_TEXT = ASSORT_TEXT_1;

    private Paint mPaint = new Paint();
    private int mSelectIndex = -1;
    private OnTouchAssortListener mListener = null;
    private Activity mAttachActivity;
    PopupWindow mPopupWindow = null;
    View layoutView;
    TextView text;
    ImageView imageview;

    public SlideBar(Context context){
        this(context,null);
    }

    public SlideBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SlideBar(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        mAttachActivity = (Activity)context;
        init(context);
    }
    private void init(Context context) {
        layoutView = LayoutInflater.from(context).inflate(R.layout.alert_dialog_menu_layout, null);
        text = (TextView) layoutView.findViewById(R.id.content);
        imageview = layoutView.findViewById(R.id.image_view);
    }

    public void setOnTouchAssortListener(OnTouchAssortListener listener) {
        this.mListener = listener;
    }

    public void setAllType(boolean isAll) {
        if (isAll) {
            ASSORT_TEXT = ASSORT_TEXT_2;
        } else {
            ASSORT_TEXT = ASSORT_TEXT_1;
        }
        invalidate();
    }


    private Bitmap mBitmap;

    private Bitmap loadBitmap() {
        if (mBitmap != null && !mBitmap.isRecycled()) {
            return mBitmap;
        }

        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_slide_recent);
        return mBitmap;
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        int nHeight = getHeight();
        int hWidth = getWidth();
        int nAssortCount = ASSORT_TEXT.length;

//        if (mIsAllType) {
//            nAssortCount += 2;
//        }
        int nInterval = nHeight / (nAssortCount + 2);


//        int nInterval = (int)(12 * getResources().getDisplayMetrics().density);
        int startY = nInterval;


        for (int i = 0; i < nAssortCount; i++){
            mPaint.setAntiAlias(true); // 抗锯齿
            mPaint.setTypeface(Typeface.DEFAULT_BOLD); // 默认粗体
            mPaint.setColor(Color.parseColor("#3F3F3F")); // 白色
            mPaint.setTextSize(10 * getResources().getDisplayMetrics().density);
//            if (i == mSelectIndex){
//                // 被选择的字母改变颜色和粗体
//                mPaint.setColor(Color.parseColor("#3399ff"));
//                mPaint.setFakeBoldText(true);
//            }

            if (ASSORT_TEXT[i].equals("$")) {
                Bitmap bitmap = loadBitmap();
                if (bitmap != null) {
                    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    int left = (getWidth() - bitmap.getWidth()) / 2;
                    int top = startY - nInterval / 2;
                    Rect sourceRect = new Rect(left, top, left + bitmap.getWidth(), top + bitmap.getHeight());
                    canvas.drawBitmap(bitmap, rect, sourceRect, mPaint);
                }
            } else {

                // 计算字母的X坐标
                float xPos = hWidth / 2 - mPaint.measureText(ASSORT_TEXT[i]) / 2;

                float yPos = startY + nInterval * i; // 计算字母的Y坐标
                canvas.drawText(ASSORT_TEXT[i], xPos, yPos, mPaint);
            }
            mPaint.reset();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //判断是哪一个字母被点击了
        int nIndex = (int) (event.getY() / getHeight() * ASSORT_TEXT.length);
        if (nIndex >= 0 && nIndex < ASSORT_TEXT.length){
            switch (event.getAction()){
                case MotionEvent.ACTION_MOVE:
                    // 如果滑动改变
                    if (mSelectIndex != nIndex){
                        mSelectIndex = nIndex;
                        showCharacter(ASSORT_TEXT[mSelectIndex]);
                        if (mListener != null){
                            mListener.onTouchAssortListener(ASSORT_TEXT[mSelectIndex]);
                        }
                    }
                    break;
                case MotionEvent.ACTION_DOWN:
                    mSelectIndex = nIndex;
                    showCharacter(ASSORT_TEXT[mSelectIndex]);
                    if (mListener != null){
                        mListener.onTouchAssortListener(ASSORT_TEXT[mSelectIndex]);
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    disShowCharacter();
                    mSelectIndex = -1;
                    break;
            }
        } else {
            mSelectIndex = -1;
            disShowCharacter();
        }
        invalidate();
        return true;
    }

    private void disShowCharacter() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
            mPopupWindow=null;
        }
    }

    /**
     * 显示弹出的字符
     * @param string
     */
    private void showCharacter(String string){

//        if (mPopupWindow != null){
//            text.setText(string);
//
//        } else{
//            mPopupWindow = new PopupWindow(layoutView, 100, 100, false);
//            mPopupWindow.showAtLocation(mAttachActivity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
//        }
//        text.setText(string);
//        if (string.equals("$")) {
//            text.setText("");
//            imageview.setVisibility(VISIBLE);
//        } else {
//            imageview.setVisibility(GONE);
//        }
    }
}
